package com.winskysoft.tool;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.winskysoft.database.DatabaseManager;
import com.winskysoft.entity.Column;
import com.winskysoft.entity.Table;

public class DatabaseTool {
	private static List<Table> list = new ArrayList<Table>();
	
	private static List<Table> selectList=new ArrayList<Table>();

	public static void load() throws SQLException {
		list.clear();
		DatabaseManager databaseManager = DatabaseManager.getDatabaseManager();
		List<String[]> set = databaseManager.queryList("show tables;");
		for (String[] s : set) {
			Table table = new Table();
			list.add(table);
			table.setName(s[0]);
			table.setTable(s[0]);
			List<Column> columnList = new ArrayList<Column>();
			table.setColumn(columnList);
			List<String[]> list = databaseManager.queryList("describe " + s[0]);
			for (String[] x : list) {
				Column column = new Column();
				column.setName(x[0]);
				column.setColumn(x[0]);
				column.setType(x[1]);
				if(!skip(column)){
					columnList.add(column);
				}
			}
		}
	}
	
	/**
	 * 跳过
	 * @param column
	 * @return
	 */
	public static boolean skip(Column column){
		if(column.getColumn().equals("creator")){
			return true;
		}else if(column.getColumn().toLowerCase().equals("updator")){
			return true;
		}else if(column.getColumn().toLowerCase().equals("createdate")){
			return true;
		}else if(column.getColumn().toLowerCase().equals("updatedate")){
			return true;
		}else if(column.getColumn().toLowerCase().equals("isdelete")){
			return true;
		}else if(column.getColumn().toLowerCase().equals("creatorname")){
			return true;
		}else if(column.getColumn().toLowerCase().equals("updatorname")){
			return true;
		}
		
		return false;
	}
	
	public static Table[] getList(){
		return list.toArray(new Table[0]);
	}
	
	public static Table[] getSelectList(){
		return selectList.toArray(new Table[0]);
	}
	
	public static void setSelect(String[] select){
		selectList.clear();
		for(int i=0;i<list.size();i++){
			for(int j=0;j<select.length;j++){
				if(list.get(i).getName().equals(select[j])){
					selectList.add(list.get(i));
				}
			}
			
		}
	}
}
